package cn.felord.debezium.utils;

import cn.felord.debezium.common.Watch;
import cn.felord.debezium.service.impl.TcpActServiceIml;
import cn.felord.debezium.task.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.mina.core.session.IoSession;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

import static cn.felord.debezium.utils.NumberUtils.HexStrToByteArray;

@Component
@Slf4j
public class AuToSend {
    public R sendEquip(String sendStr, String equipCode) throws IOException {
        log.info("进入autoSend" + sendStr);
        log.info("现存池polls:" + Watch.poolsHash.size());
        IoSession ioSession = Watch.poolsHash.get(equipCode);
        if(null != ioSession){
//            log.info("1定点主动发送给remoteAdress：" + ioSession.getRemoteAddress());
//            log.info("2定点主动发送给localAddress："+ioSession.getLocalAddress());
            if(ioSession.isConnected()){
                TcpActServiceIml.sendMsg(ioSession,sendStr);
//                byte[] byteArray = HexStrToByteArray(sendStr);
//                ioSession.write(byteArray);
               return R.success("下发成功!");
            }else{
                log.info("未发送,会话关闭！");
                return R.failure("session会话关闭，发送失败");
            }
        }else {
            System.out.println("无对应，设备编码：" + equipCode);
            return R.failure("下发失败，会话池中没有" + equipCode + "对应会话。");
        }
    }
}
